package com.fc.v2.controller;

import com.fc.v2.common.base.BaseController;

import com.fc.v2.common.domain.AjaxResult;
import com.fc.v2.util.ImageUtil;
import com.fc.v2.util.StringUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.fc.v2.common.domain.AjaxResult.successData;

/**
 * @author fuce
 * @ClassName: ImagesController
 * @date 2019-10-21 00:15
 */
@Api(tags = "图片上传下载")
@Controller
@RequestMapping("/ImagesController")
public class ImagesController extends BaseController {
    @Value("${imagePath.carousel}")
    private String carouselPath;
    @Value("${imagePath.mechanism}")
    private String mechanismPath;
    @Value("${imagePath.course}")
    private String coursePath;
    @Value("${imagePath.lecturer}")
    private String lecturerPath;
    @Value("${imagePath.campus}")
    private String campusPath;

    @RequestMapping("/uploadImg/{type}")
    @ResponseBody
    public AjaxResult upload(@RequestParam(value = "file") MultipartFile multipartFile, @PathVariable(value = "type") Integer type) {
        String basePath = "";
        if (type == 0) basePath = carouselPath;
        else if (type == 1) basePath = mechanismPath;
        else if (type == 2) basePath = coursePath;
        else if (type == 3) basePath = campusPath;
        else if (type == 4) basePath = lecturerPath;
        String imgName = ImageUtil.upload(multipartFile, basePath);
        if (StringUtils.isNotEmpty(imgName)) return successData(1, imgName);
        else return error(0, "上传失败");
    }

    @RequestMapping("/downloadImg//{type}/{fileName}")
    @ResponseBody
    public void download(@PathVariable(value = "fileName") String fileName, @PathVariable(value = "type") Integer type) {
        String basePath = "";
        if (type == 0) basePath = carouselPath;
        else if (type == 1) basePath = mechanismPath;
        else if (type == 2) basePath = coursePath;
        else if (type == 3) basePath = campusPath;
        else if (type == 4) basePath = lecturerPath;
        try {
            ImageUtil.download(basePath + fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
